#include "ShootMachine.h"


ShootMachine::ShootMachine(void)
{
	shootSpeed = 200;
	ostatniStrzal = 0;
	time = sf::Time::Zero;
	time = shootSpeedColck.restart();
	
}


void ShootMachine::Attack(Rotacion mRotacion, sf::Vector2f mPosition)
{

	if(time.asSeconds() + 1 < shootSpeedColck.getElapsedTime().asSeconds())
	{
	Shoot * mShoot = new Shoot();
	mShoot->mPosition = mPosition;
	mShoot->mRotacion = mRotacion;

	mShoot->Shape.setPosition(mPosition);
	mShoot->Shape.setSize(sf::Vector2f(5,5));

	mShoot->erase = false;

	tShoots.push_back(mShoot);

	time = shootSpeedColck.getElapsedTime();
	}
	
}

void ShootMachine::update(sf::Time& elapsedTime)
{
	for(int i = 0; i< tShoots.size(); i++)
	{
		tShoots[i]->update(shootSpeed, elapsedTime);
		if(tShoots[i]->erase)
		{
			delete tShoots[i];
			tShoots.erase(tShoots.begin()+i);
		}
	}
}

void ShootMachine::draw(sf::RenderWindow* mWindow)
{
	for(int i = 0; i< tShoots.size(); i++)
	{
		mWindow->draw(tShoots[i]->Shape);
	}
}


void Shoot::update(float& shootSpeed, sf::Time& elapsedTime)
{
	switch (mRotacion)
	{
	case Up: mPosition.y -= shootSpeed * elapsedTime.asSeconds();
		break;
	case Right: mPosition.x += shootSpeed* elapsedTime.asSeconds();
		break;
	case Down:  mPosition.y += shootSpeed* elapsedTime.asSeconds();
		break;
	case Left: mPosition.x -= shootSpeed* elapsedTime.asSeconds();
		break;
	default:
		break;
	}

	Shape.setPosition(mPosition);

	if(mPosition.x > 560 || mPosition.x < 40)
		erase = true;
	if(mPosition.y > 560 || mPosition.y < 40)
		erase = true;
}
